package de.uni_mannheim.informatik.ai.dame_ai.util;

import java.util.Collection;
import java.util.LinkedList;

public class XmlElement{

	private Tag tag;
	private String textContent;
	private Collection<XmlElement> children; 

	
	public XmlElement(Tag tag){
		this.tag=tag;
	}
	
	public XmlElement(String tagName){
		this(tagName, null);
	}
	
	public XmlElement(String tagName, String textContent){
		this(tagName,-1,textContent);
	}
	
	public XmlElement(String tagName,int id, String textContent){
		this.tag=new Tag(tagName);
		this.tag.setId(id);
		this.textContent=textContent;
	}
	
	
	public XmlElement(Tag tag, String content){
		this.tag=tag;
		this.textContent=content;
	}

	public XmlElement(Tag tag, Collection<XmlElement> children){
		this.tag=tag;
		this.children=children;
	}

	/**
	 * Returns a textual representation of this XmlElemt with it's id attribute and </br>
	 * <b>without</b> content, other attributes
	 * and children.<br/>
	 * Example:<br/>
	 * elementX.toString():<br/> 
	 * "<PapaPanda id='123' color1='black', color2='white'><LittlePanda/></PapaPanda>"<br/>
	 * ---------------------<br/>
	 * elementX.getStubRep():<br/>
	 * "<PapaPanda id='123'></PapaPanda>"<br/>
	 */
	public String getStubRep(){
		return tag.opener()+tag.closer();
	}
	
	
	public String getTagName(){
		return this.getTag().getName();
	}
	
	/**
	 * @return the number of children of this element.
	 */
	public int countChildren(){
		int amount=0;
		if(children!=null)
			amount=children.size();
		return amount;
	}
	
	public XmlElement getChild(String tagName){
		for(XmlElement child: children){
			if(child.getTagName().equals(tagName))
				return child;
		}
		return null;
	}
	
	public int getId(){
		return getTag().getId();
	}
	
	public Tag getTag() {
		return tag;
	}
	
	public void addChild(String tagName, String textContent){
		addChild(tagName,-1, textContent);
	}
	
	public void addChild(String tagName){
		addChild(tagName,-1, null);
	}
	
	public void addChild(XmlElement child){
		if(this.children==null)
			this.children=new LinkedList<XmlElement>();
		children.add(child);
	}
	
	public void addChild(String tagName,int id, String textContent){
		if(this.children==null)
			this.children=new LinkedList<XmlElement>();
		children.add(new XmlElement(tagName, id, textContent));
	}
	

	@Override
	public String toString(){
		String rep=this.getTag().opener();
		if(this.hasChildren()){
			rep+="\n";
			for( XmlElement child: this.children)
				rep+=child.toString()+"\n";
		}
		else{
			if(textContent!=null)
				rep+=textContent;
		}
		rep+=this.getTag().closer();
		return rep;
	}
	
	/**
	 * @return The text content of the XmlElement. If there's no text content: null
	 */
	public String getContent() {
		return textContent;
	}
	
	/**
	 * @return the children of this element.
	 */
	public Collection<XmlElement> getChildren(){
		return children;
	}
	
	/**
	 * @return true if the XmlElement has child elements, else  false
	 */
	public boolean hasChildren(){
		if(children!=null && !children.isEmpty())
			return true;
		else 
			return false;
	}
}